const goodController = require("../controllers/good");
const { checkRole } = require("../middleware/role");
const { checkTokenAdmin } = require("../middleware/jwt");
const config = require("../config/config");
const path = require("path");
const multer = require("multer");
const userOrderController = require("../controllers/appOrder");
module.exports = (app) => {
  // 权限管理 需要注册中间件 登录
  app.use("/admin/good", checkTokenAdmin);
  app.use("/admin/tag", checkTokenAdmin);
  app.use("/admin/order", checkTokenAdmin);
  // 这些都需要权限
  app.use("/admin/good", (req, res, next) =>
    checkRole(req, res, next, "product")
  );
  app.use("/admin/tag", (req, res, next) =>
    checkRole(req, res, next, "product")
  );
  app.use("/admin/order", (req, res, next) =>
    checkRole(req, res, next, "order")
  );
  // 商品增删改查
  app.get("/admin/good", goodController.getAllGood);
  app.post("/admin/good", goodController.createGood);
  app.get("/admin/good/:id", goodController.getGoodById);
  app.put("/admin/good/:id", goodController.updateGood);
  app.delete("/admin/good/:id", goodController.deleteGood);
  // 分类分页查询商品
  app.post("/admin/good/filter", goodController.getFilterGood);
  // 标签的增删改查
  app.get("/admin/tag", goodController.getAllTag);
  app.post("/admin/tag", goodController.createTag);
  app.get("/admin/tag/:id", goodController.getTagById);
  app.put("/admin/tag/:id", goodController.updateTag);
  app.delete("/admin/tag/:id", goodController.deleteTag);

  // 订单的操作
  app.get("/admin/order", userOrderController.getAllOrders);
  //   发货
  app.post("/admin/order/shipOrder/:id", userOrderController.shipOrder);
  // 完成订单
  app.post("/admin/order/finish/:id", userOrderController.finishOrder);
  // 图片上传
  // 设置 Multer 存储配置
  const storage = multer.diskStorage({
    destination: function (req, file, cb) {
      cb(null, "public/images"); // 文件存储目录
    },
    filename: function (req, file, cb) {
      cb(null, Date.now() + path.extname(file.originalname)); // 文件名
    },
  });
  // 初始化 Multer 上传中间件
  const upload = multer({ storage: storage });
  app.post(
    "/admin/good/upload",
    upload.single("image"),
    goodController.imageListUpload
  );
};
